var love = 0;
$(function () {

    /*供货商所有单据*/
    $('#firstDate').datetimepicker({
        //viewMode: 'day',
        format: 'YYYY-MM-DD'
    });
    $('#secondDate').datetimepicker({
        format: 'YYYY-MM-DD',
        useCurrent: false //Important! See issue #1075
    });
    $("#firstDate").on("dp.change", function (e) {
        $('#secondDate').data("DateTimePicker").minDate(e.date);
    });
    $("#secondDate").on("dp.change", function (e) {
        $('#firstDate').data("DateTimePicker").maxDate(e.date);
    });


    $('#biao').bootstrapTable({
        url: '${pageContext.request.contextPath}/../../lin/query2',

        columns: [
            {
                checkbox : true
            },{
                field: 'id',
                title: 'id',
                visible : false,
            },
            {
                field: 'number',
                title: '货品编号',
                visible : false,
            },
            {
                field: 'name',
                title: '货品名称'
            }, {
                field: 'comptype.name',
                title: '单位'
            }, {
                field: 'spec',
                title: '规格型号'
            }, {
                field: 'colortype.name',
                title: '颜色'
            }, {
                field: 'bprice',
                title: '单价'
            }, {
                field: 'sum',
                title: '数量'
            }, {
                field: 'xx',
                title: '总金额',
                formatter: function (value, row, index) {

                    var jia = row.bprice * row.sum;
                    return jia;
                }
            }
        ],
        height: 450,
        striped:true,
        onDblClickRow: function (row, $element, field) {

            $("#myModal2").modal("show");
            $("#hpbh").text(row.number);
            $("#ggxh").text(row.spec);
            $("#lb").text("");
            $("#dqkc").text("");

            $("#hpmc").text(row.name);
            $("#dw").text(row.comptype.name);
            $("#ys").text(row.colortype.name);
            $("#ckjg").text(row.bprice+"元");

            $("#scjj").text("");
            $("#ckjj").val(row.bprice);
            $("#sum").val(row.sum);
            $("#ddd").attr("onclick","ti2()")
        },
    });

    $("#lin1").bootstrapTable({
        url: '${pageContext.request.contextPath}/../../lin/query1',
        columns: [
            {
                field: 'id',
                title: 'id',
                visible : false,
            },
            {
                field: 'name',
                title: '货品名称'
            }, {
                field: 'comptype.name',
                title: '单位'
            }, {
                field: 'spec',
                title: '规格型号'
            }, {
                field: 'colortype.name',
                title: '颜色'
            }, {
                field: 'bprice',
                title: '单价'
            }, {
                field: 'sum',
                title: '数量'
            }, {
                field: 'xx',
                title: '总金额',
                formatter: function (value, row, index) {
                    var jia = row.bprice * row.sum;
                    return jia;
                }
            }
        ],
        height: 600,
        striped:true,
    })


    $('#jiu2').bootstrapTable({
        url: '${pageContext.request.contextPath}/../../Qbb/tui',
        method: "post",
        striped:true,
        contentType: "application/x-www-form-urlencoded",
        onDblClickRow: function (row, $element, field) {
            $("#myModal2").modal("show");
            $("#hpbh").text(row.number);
            $("#ggxh").text(row.specid);
            $("#lb").text(row.commtype.name);
            $("#dqkc").text(row.num+""+ row.comptype.name);

            $("#hpmc").text(row.name);
            $("#dw").text(row.comptype.name);
            $("#ys").text(row.colortype.name);
            $("#ckjg").text(row.money+"元");

            $("#scjj").text(row.money);
            $("#ckjj").val(row.money);
            $("#sum").val("1");

            $("#lei1").val(row.commtype.id);
            $("#yan1").val(row.colortype.id);
            $("#dan1").val(row.comptype.id);
            $("#ddd").attr("onclick","ti1()")
            love = row.num

        },
        columns: [{
            field: 'id',
            title: 'id',
            visible : false,
        },{
            field: 'number',
            title: '产品编号'
        }, {
            field: 'name',
            title: '产品名称',
        },{
            field: 'commtype.id',
            title: '类别id',
            visible : false,
        }, {
            field: 'commtype.name',
            title: '类别名',
        },{
            field: 'comptype.id',
            title: '单位id',
            visible : false,
        },{
            field: 'comptype.name',
            title: '单位',
            // formatter : function(value, row, index) {
            //     if(row.sex==1){
            //         return "男";
            //     }else if(row.sex==2){
            //         return "女";
            //     }
            // }
        }, {
            field: 'specid',
            title: '规格'
        }, {
            field: 'colortype.id',
            title: '颜色id',
            visible : false,
        },{
            field: 'colortype.name',
            title: '颜色'
        }, {
            field: 'miniin',
            title: '最低库存',
            visible : false,
        },{
            field: 'money',
            title: '参考进价',
        },{
            field: 'eprice',
            title: '卖家',
            visible : false,
        }, {
            field: 'num',
            title: '库存',
            //visible : false,
        }],
        queryParamsType: '',
        //修改分页的默认对数
        //offset,limit
        queryParams: queryParams,
        /* //是否显示刷新按钮。
         showRefresh:true,
         //是否显示内容列下拉框。
         showColumns:true,*/
        height: 394,
        pageNumber: 1,
        pageSize: 9,
        pagination: true,
        sidePagination: 'server',
    })

    $('#table').bootstrapTable({
        url: '${pageContext.request.contextPath}/../../j/query3',
        columns: [

            {
                field: 'order',
                title: '订单号'
            }, {
                field: 'supplier.name',
                title: '供货商名称'
            },{
                field: 'date',
                title: '订单时间'
            },  {
                field: 'ietype.type',
                title: '类型'
            },  {
                field: 'money',
                title: '金额'
            },{
                field: 'staff.name',
                title: '经办人'
            }],




        method:'post',
        contentType:"application/x-www-form-urlencoded",

        queryParamsType:'',
        detailView:true,
        queryParams:function(params){
            var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                'pageSize': params.pageSize,  //页面大小
                'pageNumber': params.pageNumber, //页码
                'firstDate':$("#firstDate").val(),
                'secondDate':$("#secondDate").val(),
                'supplierr':$("#supplierr").combogrid('getValue')

            };
            return temp;
        },onDblClickRow: function (row, $element, field) {
           $.post("${pageContext.request.contextPath}/../../lin/datailsadd",{"order":row.order},function (data) {
               sweetAlert("添加成功");
                $("#biao").bootstrapTable("load",data);
           })
        },

        height:600,
        pageList:[10,20],
        pageNumber:1,
        pageSize:10,
        pagination:true,
        sidePagination:'server',



        //当点击详细图标展开详细页面的时候触发。
        onExpandRow: function (index, row, $detail) {
            var table = $detail.html("<table></table>").find("table") ;
            table.bootstrapTable({
                url: '${pageContext.request.contextPath}/../../Qbb/QqueryByOrder?order1='+row.order+'',

                columns: [{
                    field: 'number',
                    title: '货品编号'
                },{
                    field: 'name',
                    title: '货品名称'
                },{
                    field: 'comptype.name',
                    title: '单位'
                },{
                    field: 'money',
                    title: '单价'
                },{
                    field: 'num',
                    title: '数量'
                },{
                    field: 'specid',
                    title: '规格'
                },{
                    field: 'colortype.name',
                    title: '颜色'
                },
                    {
                        field:'xx',
                        title:'总金额',
                        formatter: function (value, row, index) {
                            var jia = row.money * row.num;

                            return jia;
                        }
                    }],



            }) ;
        }

    });


})


/*供货商供货情况*/
function tome(){
    var opt = $("#table").bootstrapTable('getOptions') ;
    var firstDate = $("#firstDate").val() ;
    var secondDate = $("#secondDate").val() ;
    var supplierr = $('#supplierr').combogrid('getValue');


    $.post('${pageContext.request.contextPath}/../../j/query3',{'supplierr':supplierr,'pageNumber':opt.pageNumber,'pageSize':opt.pageSize,'firstDate':firstDate,'secondDate':secondDate},function(data){
        //重新给table绑定数据5
        $("#table").bootstrapTable("load",data) ;
    }) ;

}




function search() {

    var opt = $("#jiu2").bootstrapTable('getOptions');

    var name = $("#name").val();
    var lei = $('#lei').combogrid('getValue');
    var  ghs = $("#ghs").combogrid('getValue');
    var ck = $("#ck").combogrid('getValue');
    //alert(name) ;

    $.post('${pageContext.request.contextPath}/../../Qbb/tui', {
        'lei':lei,
        'name': name,
        'supplierid':ghs,
        'warehouseid':ck,
        'pageSize': opt.pageSize,
        'pageNumber': opt.pageNumber
    }, function (data) {

        //重新给table绑定数据
        $("#jiu2").bootstrapTable("load", data);
    });
}

function queryParams(params) {
    var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
        'pageSize': params.pageSize,  //页面大小
        'pageNumber': params.pageNumber, //页码
        'name': $("#name").val(),
        'lei' :$ ('#lei').combogrid('getValue'),
        'ghs' :$("#ghs").combogrid('getValue'),
        'ck' : $("#ck").combogrid('getValue'),

    };
    return temp;
};


function tui(){
    var ghs = $('#ghs').combogrid('getValue');
    var ck = $('#ck').combogrid('getValue');
    if(ghs==null || ghs==''){
        sweetAlert("请选择退货供货商");
        return false;
    }else if(ck===null || ck==''){
        sweetAlert("请选择退货仓库");
        return false;
    }
    var opt = $("#jiu2").bootstrapTable('getOptions');

    var name = $("#name").val();
    var lei = $('#lei').combogrid('getValue');
    $.post('${pageContext.request.contextPath}/../../Qbb/tui', {
        'lei':lei,
        'name': name,
        'supplierid':ghs,
        'warehouseid':ck,
        'pageSize': opt.pageSize,
        'pageNumber': opt.pageNumber
    }, function (data) {
        //重新给table绑定数据
        $("#jiu2").bootstrapTable("load", data);
    });
    $("#myModal").modal("show");

}


function save2(){
    var gong = $('#ghs').combogrid('getValue');
    var changku = $("#ck").combogrid('getValue');
    var tim = $("#time").val();
    var order = $("#order").text();
    var jingbanren = $("#jbr").combogrid('getValue');

    var zhong = $("#zhong").text();

    var tt = time();

    $.ajax({
        type : "post",
        url : "${pageContext.request.contextPath}/../../lin/chanumber",
        async : false,
        datatype : "json",
        success : function(data) {
            if(data==null || data==''){
                sweetAlert("单据中没有业务发生无法保存！");
                return false;
            }else{
                if(gong==null || gong==''){
                    sweetAlert("请选择供货商！");
                    return false;
                }else if(changku==null || changku==''){
                    sweetAlert("请选择仓库！");
                    return false;
                }else if(jingbanren==null || jingbanren==''){
                    sweetAlert("请选择经办人！");
                    return false;
                }

                if(tim.length == 10){
                    tt = tim;
                }
                var typeid = 2;


                $.post("${pageContext.request.contextPath}/../../j/add",{"order":order,"supplierid":gong,"date":tt,"money":zhong,"typeid":typeid,"agent":jingbanren,"warehouseid":changku},function (data) {
                    if(data!=0){

                        $.post("${pageContext.request.contextPath}/../../commodity/tui",function (data) {
                            if(data!=123){
                                sweetAlert(data);
                            }else{
                                $.post("${pageContext.request.contextPath}/../../lin/qing",function (data) {
                                    $("#biao").bootstrapTable("load",data);
                                    sweetAlert("提交成功")
                                    Win10_child.newMsg('提示','退货已成功！');

                                    Win10_child.close();


                                })
                            }


                        })










                    }else{
                    }
                })

            }
        }
    });









}
// 获取当前时间
function time(){
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var time = year + seperator1 + month + seperator1 + strDate;
    return time;
}

function ti1() {
    console.log(love)
    var number = $("#hpbh").text();
    var name = $("#hpmc").text();
    var companyid = $("#dan1").val();
    var spec = $("#ggxh").text();
    var colorid = $("#yan1").val();
    var bprice = $("#ckjj").val();
    var sum = $("#sum").val();
    var type = "1";

    if(bprice==null || bprice==""){
        sweetAlert("请输入进货价格");
        return false;
    }
    if(sum==null || sum==""){
        sweetAlert("请输入进货数量");
        return false;
    }
    if(sum>love){
        sweetAlert("您输入的数量超过了您的库存数量！");
        return false;
    }
    $.post("${pageContext.request.contextPath}/../../lin/add",{"number":number,"name":name,"companyid":companyid, "spec":spec,"colorid":colorid,"bprice":bprice,"sum":sum,"type":type},function (data) {
        $("#lin1").bootstrapTable("load",data);
    })

    $("#myModal2").modal("hide")
}

function  ti2() {
    var number = $("#hpbh").text();
    var bprice = $("#ckjj").val();
    var sum = $("#sum").val();

    if(bprice==null || bprice==""){
        sweetAlert("请输入进货价格");
        return false;
    }
    if(sum==null || sum==""){
        sweetAlert("请输入进货数量");
        return false;
    }
    var arr = $("#jiu2").bootstrapTable('getData');

    $.each(arr,function (i,e) {

        if(number == arr[i].number){
            if(sum>arr[i].num){
                sweetAlert("您输入的数量大于了您的库存数量！");
                return false;
            }else{
                $.post("${pageContext.request.contextPath}/../../lin/updsum",{"number":number,"bprice":bprice,"sum":sum},function (data) {
                    $("#biao").bootstrapTable("load",data);
                    console.log("lll")
                })
                $("#myModal2").modal("hide")
            }
        }

    })



    var timeout=setTimeout(function () {
        var arr1 = $("#biao").bootstrapTable('getData');
        var zhong = 0;
        $.each(arr1,function(i,e){
            var sum = arr1[i].sum;
            var qian = arr1[i].bprice;
            zhong+=sum*qian;
        })

        $("#zhong").text(zhong);
    }, 100);
}

function dan(){
    $("#myModal1").modal("show");
}